package edu.njucm.herbiv.controller

import edu.njucm.herbiv.utils.annotation.LogRequest
import edu.njucm.herbiv.pojo.entity.Chemical
import edu.njucm.herbiv.pojo.entity.Formula
import edu.njucm.herbiv.pojo.entity.Protein
import edu.njucm.herbiv.pojo.entity.Tcm
import edu.njucm.herbiv.service.ChemicalService
import edu.njucm.herbiv.service.FormulaService
import edu.njucm.herbiv.service.ProteinService
import edu.njucm.herbiv.service.TcmService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.util.ResourceUtils
import org.springframework.web.bind.annotation.*
import java.io.File

/**
 * @Author ZhouHang
 * @Date 24/5/11 011
 */
@RestController
@RequestMapping("/api")
class BaseController @Autowired constructor(
    private val chemicalService: ChemicalService,
    private val proteinService: ProteinService,
    private val tcmService: TcmService,
    private val formulaService: FormulaService
){
    @GetMapping("/hello")
    fun hello(msg: String): String {
        val path = ResourceUtils.getURL("classpath:").path
        val f = File(path)
        return "Hello, ${ f.absolutePath } ${System.getProperty("user.dir")}"
    }

    @GetMapping("/chemical/list")
    @LogRequest
    fun chemicalList(
        @RequestParam(value = "type") type: String?,
        @RequestParam(value = "content") content: String?,
        @RequestParam(value = "page") page: Int,
        @RequestParam(value = "size") size: Int
    ): Page<Chemical> {
        return chemicalService.list(type, content, page, size)
    }

    @GetMapping("/protein/list")
    @LogRequest
    fun proteinList(
        @RequestParam(value = "type") type: String?,
        @RequestParam(value = "content") content: String?,
        @RequestParam(value = "page") page: Int,
        @RequestParam(value = "size") size: Int
    ): Page<Protein> {
        return proteinService.list(type, content, page, size)
    }

    @GetMapping("/tcm/list")
    @LogRequest
    fun tcmList(
        @RequestParam(value = "type") type: String?,
        @RequestParam(value = "content") content: String?,
        @RequestParam(value = "page") page: Int,
        @RequestParam(value = "size") size: Int
    ): Page<Tcm> {
        return tcmService.list(type, content, page, size)
    }

    @GetMapping("/formula/list")
    @LogRequest
    fun formulaList(
        @RequestParam(value = "type") type: String?,
        @RequestParam(value = "content") content: String?,
        @RequestParam(value = "page") page: Int,
        @RequestParam(value = "size") size: Int
    ): Page<Formula> {
        return formulaService.list(type, content, page, size)
    }
}
